package com.android.takegism.utils;

import android.util.Log;

import androidx.annotation.Nullable;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Random;

public class EasyPuzzle extends PuzzleAbstract{

    private static Random random = new Random();


    static {
        errorAnswers = new HashSet<>();
        for (int i = 0;i<100;i++){
            int a = random.nextInt(100);
            int b = random.nextInt(100);
            int typ = random.nextInt(3);
            Integer ans;
            switch (typ){
                case 0:
                    ans = a+b;
                    break;
                case 1:
                    ans = a-b;
                    break;
                case 2:
                    ans = a*b;
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + typ);
            }
            System.out.println("预计下一条出错");
            errorAnswers.add(""+ans);
        }
        errorAnswersList = new LinkedList<>(errorAnswers);
    }

    public EasyPuzzle(@Nullable Clue clue) {
        super(clue);
        int a = random.nextInt(100);
        int b = random.nextInt(100);
        int typ = random.nextInt(3);
        int ans;
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("请输入"+a);
        switch (typ){
            case 0:
                ans = a+b;
                stringBuffer.append("加"+b);
                break;
            case 1:
                ans = a-b;
                stringBuffer.append("减"+b);
                break;
            case 2:
                ans = a*b;
                stringBuffer.append("乘"+b);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + typ);
        }
        stringBuffer.append("的结果？");
        setQuestion(stringBuffer.toString());
        setAnswer(ans+"");
        Log.d("ANSWER",ans+"");
        setHardLevel(1);
        setType("Easy");
    }

    @Override
    public Object check(Object answer) throws Exception {
        System.out.println(answer+" "+getAnswer());
        if (answer instanceof String){
            if (answer.toString().equals(getAnswer())){
                LinkedList<Clue> easy_clues = (LinkedList<Clue>) MyContext.getInstance().getAllClues("update_clues");
                if(easy_clues==null){
                    easy_clues = new LinkedList<Clue>();
                }
                easy_clues.add(clue);
                MyContext.getInstance().setAllClues("update_clues",easy_clues);
                return clue.clue;
            }else{
                return null;
            }
        }
        throw new Exception("程序错误");
    }
}
